package org.bob.learn.web.model;

import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

@Data
@Tag(name = "响应结果",description = "响应结果实体类")
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(name = "跟踪标识" ,type = "string")
    private String traceId;

    @Schema(name = "响应编码" ,type = "integer")
    private Integer code;

    @Schema(name = "响应消息" ,type = "string")
    private String msg;

    @Schema(name = "响应数据" ,type = "string")
    private T data;

    public static boolean isSuccess(Result<?> result) {
        return "200".equals(result.getCode());
    }

    public static <T> Result<T> Success(T data){
        Result<T> result = new Result<T>();
        result.setCode(200);
        result.setMsg("success");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> Success(Class<T> clazz){
        Result<T> result = new Result<T>();
        result.setCode(200);
        result.setMsg("success");
        return result;
    }

    public static <T> Result<T> Failure(String msg){
        Result<T> result = new Result<T>();
        result.setCode(100000);
        result.setMsg(msg);
        return result;
    }

    public boolean isSuccess(){
        return 200==code;
    }
}
